module WhoCharted
  module ChartFeatures
    module AxisRange
      module ClassMethods
        def accepted_arguments
          super + [:axis_range]
        end

        def argument_dictionary
          super.merge(:axis_range => 
                      {:key => 'chxr', :method => :gather_axis_range})
        end
      end

      def self.included base
        base.extend ClassMethods
      end

      # TODO: the index argument for these ranges as sent to google feels clunky.
      # it can probably be matched and determined automatically.
      def gather_axis_range
        if axis_range.class == Array
          axis_range[0].class == Array ? \
          axis_range.map {|a| a.join(',')}.join('|') : \
          axis_range.join(',')
        elsif axis_range == :auto
          data_values[0].class == Array ?
          data_values.enum_with_index.map {|v,i| "#{i},#{v.min},#{v.max}"}.join('|') : \
          "0,#{data_values.min},#{data_values.max}"
        end
      end

      attr_reader :axis_range

      private

      attr_writer :axis_range
    end
  end
end
